package com.zqserver.interfaces.user.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zqserver.constant.Constants;
import com.zqserver.interfaces.BaseController;
import com.zqserver.interfaces.user.model.TZqUser;
import com.zqserver.interfaces.user.service.UserService;
import com.zqserver.utils.ResultResponse;
import com.zqserver.utils.UtilEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by zhouquan on 2017/10/23.
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Resource
    private UserService userService;

    /**
     * 描述：新增用户
     *
     * @author zhouquan
     * @date 2018/3/22 16:01
     */
    @PostMapping
    public void addUser(@RequestBody TZqUser user) {
        ResultResponse resp = new ResultResponse();
        userService.addUser(user);
        resp.setMsg(Constants.ADD_SUCCESS);
        resp.write(response);
    }

    /**
     * 描述：删除用户
     *
     * @author zhouquan
     * @date 2018/3/22 16:01
     */
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Integer id) {
        ResultResponse resp = new ResultResponse();
        userService.deleteUser(id);
        resp.setMsg(Constants.DELETE_SUCCESS);
        resp.write(response);
    }

    /**
     * 描述：修改用户
     *
     * @author zhouquan
     * @date 2018/3/22 16:01
     */
    @PutMapping
    public void updateUser(@RequestBody TZqUser user) {
        ResultResponse resp = new ResultResponse();
        userService.updateUser(user);
        resp.setMsg(Constants.UPDATE_SUCCESS);
        resp.write(response);
    }

    /**
     * 描述：通过id查询用户
     *
     * @author zhouquan
     * @date 2018/3/22 16:02
     */
    @GetMapping("/{id}")
    public void findUserById(@PathVariable Integer id) {
        ResultResponse resp = new ResultResponse();
        TZqUser user = userService.findUserById(id);
        resp.setData(user);
        resp.setMsg(Constants.SELECT_SUCCESS);
        resp.write(response);
    }

    /**
     * 描述：分页查询用户信息
     *
     * @author zhouquan
     * @date 2018/3/22 18:56
     */
    @GetMapping
    public void listUser(@RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer size,
                         @RequestParam(required = false) String name) {
        PageHelper.startPage(page, size);
        ResultResponse resp = new ResultResponse();
        List<TZqUser> users = userService.listUser(name);
        PageInfo<TZqUser> pageInfo = new PageInfo<>(users);
        resp.setData(pageInfo);
        resp.setMsg(Constants.SELECT_SUCCESS);
        resp.writeDateFormat(response, UtilEnum.DateFormatString.yyyy_MM_dd_HHmmSS.getValue());
    }

}
